﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Models;
using System.Threading;

namespace ChinaDynastyTours.Views
{
    public partial class TeamGroupPriceManagerForm : DevExpress.XtraEditors.XtraForm
    {
        //输入参数
        public string _TourGroupID { get; set; }//团ID
        public string _TourGroupState { get; set; }//团状态

        public TeamGroupPriceManagerForm()
        {
            InitializeComponent();
        }

        private void TeamGroupPriceManagerForm_Load(object sender, EventArgs e)
        {
            lblTourGroupID.Text = _TourGroupID;
            lblTourGroupState.Text = _TourGroupState;

            hotelExpenseControl.DataSource = ExpensesCtrl.GetExpenseData(_TourGroupID, new ExpenseTypeBelongEnum[]{ExpenseTypeBelongEnum.宾馆});
        }

        private void LoadSelectedPageGridViewData()
        {
            switch (ExpenseTab.SelectedTabPageIndex)
            {
                case 0://HotelPage
                    hotelExpenseControl.DataSource = ExpensesCtrl.GetExpenseData(_TourGroupID, new ExpenseTypeBelongEnum[] { ExpenseTypeBelongEnum.宾馆 });
                    break;
                case 1://TravelPage
                    travelExpenseControl.DataSource = ExpensesCtrl.GetExpenseData(_TourGroupID, new ExpenseTypeBelongEnum[] { ExpenseTypeBelongEnum.地接社 });
                    break;
                case 2://FlightPage
                    flightExpenseControl.DataSource = ExpensesCtrl.GetExpenseData(_TourGroupID, new ExpenseTypeBelongEnum[] { ExpenseTypeBelongEnum.机票 });
                    break;
                case 3://TrainPage
                    trainExpenseControl.DataSource = ExpensesCtrl.GetExpenseData(_TourGroupID, new ExpenseTypeBelongEnum[] { ExpenseTypeBelongEnum.火车 });
                    break;
                case 4://VehiclePage
                    vehicleExpenseControl.DataSource = ExpensesCtrl.GetExpenseData(_TourGroupID, new ExpenseTypeBelongEnum[] { ExpenseTypeBelongEnum.汽车 });
                    break;
                case 5://EntertainmentPage
                    entertainmentExpenseControl.DataSource = ExpensesCtrl.GetExpenseData(_TourGroupID, new ExpenseTypeBelongEnum[] { ExpenseTypeBelongEnum.娱乐,ExpenseTypeBelongEnum.餐单});
                    break;
                case 6://EtcPage
                    etcExpenseControl.DataSource = ExpensesCtrl.GetExpenseData(_TourGroupID, new ExpenseTypeBelongEnum[] { ExpenseTypeBelongEnum.其它});
                    break;
                default: //AllPage
                    allExpenseGridControl.DataSource = ExpensesCtrl.GetExpenseData(_TourGroupID, new ExpenseTypeBelongEnum[] { 
                        ExpenseTypeBelongEnum.宾馆,ExpenseTypeBelongEnum.餐单,ExpenseTypeBelongEnum.地接社,ExpenseTypeBelongEnum.火车,ExpenseTypeBelongEnum.机票,
                        ExpenseTypeBelongEnum.其它,ExpenseTypeBelongEnum.汽车,ExpenseTypeBelongEnum.娱乐
                    });
                    break;
            }
        }
        /// <summary>
        /// 各个费用标签切换，加载各自Gridview数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ExpenseTab_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            LoadSelectedPageGridViewData();
        }

        /// <summary>
        /// 供应商选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProviderNameEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            HotelOrderServiceForm hotelServiceForm = new HotelOrderServiceForm();
            if (hotelServiceForm.ShowDialog(this) == DialogResult.OK)
            {
                DevExpress.XtraEditors.ButtonEdit btnEdit = sender as DevExpress.XtraEditors.ButtonEdit;
                btnEdit.EditValue = hotelServiceForm.ReturnProviderName;
            } 
        }


        #region '宾馆'按钮增、删、改、提交事件处理

        private void btnAddHotelExpense_Click(object sender, EventArgs e)
        {
            ExpenseEntity expense = new ExpenseEntity()
            {
                AdjustAmount = 0,
                ApplyPayTotal = 0,
                Budget = 0,
                //ExpenseID,
                PayTotal = 0,
                ProviderID = -1,
                ProviderName = "",
                ServiceName = "",
                Status = ExpenseStatusEnum.计划.ToString(),
                TourgroupID = _TourGroupID,
                TourgroupName=TeamGroupCtrl.GetTeamGroupNameById(_TourGroupID),
                TypeBelong = ExpenseTypeBelongEnum.宾馆.ToString(),
                Remark = "",
                CreateByUserName = Thread.CurrentPrincipal.Identity.Name //UserInfoCtrl.GetNameByUserName(Thread.CurrentPrincipal.Identity.Name)
            };
            ExpensesCtrl.CreateExpenseData(expense);

            //刷新界面
            LoadSelectedPageGridViewData();

            //hotelExpenseControl.DataSource = ExpensesCtrl.GetExpenseData(_TourGroupID, ExpenseTypeBelongEnum.宾馆);

            //使其处于编辑状态
            btnModifyHotelExpense.PerformClick();
        }

        private void btnModifyHotelExpense_Click(object sender, EventArgs e)
        {
            DataTable dt = (hotelExpenseGridView.DataSource as DataView).Table;
            if (btnModifyHotelExpense.Text == "保存")
            {

                hotelExpenseGridView.OptionsBehavior.Editable = false;
                btnModifyHotelExpense.Text = "修改";

                if (dt.GetChanges(DataRowState.Modified) == null) return;

                foreach (DataRow dr in dt.GetChanges(DataRowState.Modified).Rows)
                {
                    ExpenseEntity entity = new ExpenseEntity()
                    {
                        ExpenseID = int.Parse(dr["ExpenseID"].ToString()),
                        TourgroupID = lblTourGroupID.Text,
                        TourgroupName=dr["TourgroupName"].ToString(),
                        ProviderID = int.Parse(dr["ProviderID"].ToString()),
                        ProviderName = dr["ProviderName"].ToString(),
                        Budget = decimal.Parse(dr["Budget"].ToString()),
                        Status = dr["Status"].ToString(),
                        Remark = dr["Remark"].ToString(),
                        ServiceName = dr["ServiceName"].ToString(),
                        TypeBelong = ExpenseTypeBelongEnum.宾馆.ToString()
                    };
                    ExpensesCtrl.UpdateExpenseData(entity);
                }
                MessageBox.Show("保存完毕！");
            }
            else
            {
                hotelExpenseGridView.OptionsBehavior.Editable = true;
                btnModifyHotelExpense.Text = "保存";
            }

        }

        private void btnDelHotelExpense_Click(object sender, EventArgs e)
        {
            if (hotelExpenseGridView.FocusedRowHandle < 0) return;

            if (MessageBox.Show("确认删除此选中费用信息？", "删除确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int expenseID = int.Parse(hotelExpenseGridView.GetRowCellValue(hotelExpenseGridView.FocusedRowHandle, "ExpenseID").ToString());
                ExpensesCtrl.DeleteExpense(expenseID, ExpenseTypeBelongEnum.宾馆); 

                //刷新GridView
                LoadSelectedPageGridViewData();
            }
        }

        private void btnHotelExpenseSubmit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认提交全部宾馆内容进行审核吗？", "提交审核确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                for (int i = 0; i < hotelExpenseGridView.DataRowCount; i++)
                {
                    string expenseId = hotelExpenseGridView.GetRowCellValue(i, "ExpenseID").ToString();
                    ExpensesCtrl.UpdateExpenseStatusData(expenseId, ExpenseStatusEnum.待审核);
                }
                //刷新界面
                LoadSelectedPageGridViewData();
                MessageBox.Show("提交完毕！", "提交审核", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #endregion

        #region '地接社'按钮增、删、改、提交事件处理

        private void btnAddTravelExpense_Click(object sender, EventArgs e)
        {
            ExpenseEntity expense = new ExpenseEntity()
            {
                AdjustAmount = 0,
                ApplyPayTotal = 0,
                Budget = 0,
                //ExpenseID,
                PayTotal = 0,
                ProviderID = -1,
                ProviderName = "",
                ServiceName = "",
                Status = ExpenseStatusEnum.计划.ToString(),
                TourgroupID = _TourGroupID,
                TourgroupName=TeamGroupCtrl.GetTeamGroupNameById(_TourGroupID),
                TypeBelong = ExpenseTypeBelongEnum.地接社.ToString(),
                Remark = "",
                CreateByUserName = Thread.CurrentPrincipal.Identity.Name //UserInfoCtrl.GetNameByUserName(Thread.CurrentPrincipal.Identity.Name)
          };
            ExpensesCtrl.CreateExpenseData(expense);

            LoadSelectedPageGridViewData();
            //travelExpenseControl.DataSource = ExpensesCtrl.GetExpenseData(_TourGroupID, ExpenseTypeBelongEnum.地接社);

            //使其处于编辑状态
            btnModifyTravelExpense.PerformClick();
        }

        private void btnModifyTravelExpense_Click(object sender, EventArgs e)
        {
            DataTable dt = (travelExpenseGridView.DataSource as DataView).Table;
            if (btnModifyTravelExpense.Text == "保存")
            {
                travelExpenseGridView.OptionsBehavior.Editable = false;
                btnModifyTravelExpense.Text = "修改";

                if (dt.GetChanges(DataRowState.Modified) == null) return;

                foreach (DataRow dr in dt.GetChanges(DataRowState.Modified).Rows)
                {
                    ExpenseEntity entity = new ExpenseEntity()
                    {
                        ExpenseID = int.Parse(dr["ExpenseID"].ToString()),
                        TourgroupID = lblTourGroupID.Text,
                        TourgroupName=dr["TourgroupName"].ToString(),
                        ProviderID = int.Parse(dr["ProviderID"].ToString()),
                        ProviderName = dr["ProviderName"].ToString(),
                        Budget = decimal.Parse(dr["Budget"].ToString()),
                        Status = dr["Status"].ToString(),
                        Remark = dr["Remark"].ToString(),
                        ServiceName = dr["ServiceName"].ToString(),
                        TypeBelong = ExpenseTypeBelongEnum.地接社.ToString()
                    };
                    ExpensesCtrl.UpdateExpenseData(entity);
                }
                MessageBox.Show("保存完毕！");
            }
            else
            {
                travelExpenseGridView.OptionsBehavior.Editable = true;
                btnModifyTravelExpense.Text = "保存";
            }

        }
        private void btnDelTravelExpense_Click(object sender, EventArgs e)
        {
            if (travelExpenseGridView.FocusedRowHandle < 0) return;

            if (MessageBox.Show("确认删除此选中费用信息？", "删除确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                int expenseID = int.Parse(travelExpenseGridView.GetRowCellValue(travelExpenseGridView.FocusedRowHandle, "ExpenseID").ToString());
                ExpensesCtrl.DeleteExpense(expenseID, ExpenseTypeBelongEnum.地接社);

                //刷新GridView
                LoadSelectedPageGridViewData();
            }
        }
        private void btnTravelExpenseSubmit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认提交全部旅行社内容进行审核吗？", "提交审核确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                for (int i = 0; i < travelExpenseGridView.DataRowCount; i++)
                {
                    string expenseId = travelExpenseGridView.GetRowCellValue(i, "ExpenseID").ToString();
                    ExpensesCtrl.UpdateExpenseStatusData(expenseId, ExpenseStatusEnum.待审核);
                }
                //刷新界面
                LoadSelectedPageGridViewData();
                MessageBox.Show("提交完毕！", "提交审核", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //查看‘预订’信息
        private void HotelIetmOrderExpenseIDEdit_OpenLink(object sender, DevExpress.XtraEditors.Controls.OpenLinkEventArgs e)
        {
            if (e.EditValue.ToString() == "") return;
            ;
        }
        //查看‘合同’信息
        private void HotelContractPathEdit_OpenLink(object sender, DevExpress.XtraEditors.Controls.OpenLinkEventArgs e)
        {
            if (e.EditValue.ToString() == "") return;

            new ContractToolForm(e.EditValue.ToString()).ShowDialog(this); return;
        }


        #endregion

        #region '其它'按钮增、删、改、提交事件处理

        private void btnAddEtcExpense_Click(object sender, EventArgs e)
        {

        }

        private void btnDelEtcExpense_Click(object sender, EventArgs e)
        {

        }

        private void btnModifyEtcExpense_Click(object sender, EventArgs e)
        {

        }

        private void btnEtcExpenseSubmit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认提交当前全部内容进行审核吗？", "提交审核确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                for (int i = 0; i < etcExpenseGridView.DataRowCount; i++)
                {
                    string expenseId = etcExpenseGridView.GetRowCellValue(i, "ExpenseID").ToString();
                    ExpensesCtrl.UpdateExpenseStatusData(expenseId, ExpenseStatusEnum.待审核);
                }
                //刷新界面
                LoadSelectedPageGridViewData();
                MessageBox.Show("提交完毕！", "提交审核", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        } 
        #endregion


    }
}
